package com.example.group_20.controller;

import com.example.group_20.entity.Students;
import com.example.group_20.entity.Teachers;
import com.example.group_20.mapper.FilesMapper;
import com.example.group_20.service.TeacherService;
import com.example.group_20.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @author: 栋
 * @data: 2023/8/20
 * @descriptions:
 */
@RestController
@CrossOrigin
@RequestMapping("/teachers")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    @RequestMapping("login")
    public Result login(@RequestBody Map<String,String> mp){
        StringBuilder username=new StringBuilder("");
        StringBuilder pwd=new StringBuilder("");
        if(mp.containsKey("username")){
            username.append(mp.get("username"));
        }
        if(mp.containsKey("password")){
            pwd.append(mp.get("password"));
        }
        return teacherService.login(username.toString(),pwd.toString());
    }

    @RequestMapping("infor/getInfor")
    public Result getInfor(String token){
        return teacherService.getInfor(token);
    }

    @RequestMapping("logout")
    public Result logout(){
        return teacherService.logOut();
    }

    @RequestMapping("infor/showInfor")
    public Result showInfor(String token){
        return teacherService.showInfor(token);
    }

    @RequestMapping("infor/updateInfor")
    public Result updateInfor(@RequestBody Map<String,Object> mp){
        StringBuilder t_id=new StringBuilder("");
        StringBuilder t_name=new StringBuilder("");
        StringBuilder t_email=new StringBuilder("");
        StringBuilder t_tel=new StringBuilder("");
        if(mp.containsKey("t_id")){
            t_id.append(mp.get("t_id"));
        }
        if(mp.containsKey("t_name")){
            t_name.append(mp.get("t_name"));
        }
        if(mp.containsKey("t_email")){
            t_email.append(mp.get("t_email"));
        }
        if(mp.containsKey("t_tel")){
            t_tel.append(mp.get("t_tel"));
        }

        return teacherService.updateInfor(new Teachers(t_id.toString(),t_name.toString(),t_email.toString(),t_tel.toString(),null));
    }

    @RequestMapping("infor/updatePwd")
    public Result updatePwd(@RequestBody Map<String,Object> mp){
        StringBuilder t_id=new StringBuilder("");
        StringBuilder t_password=new StringBuilder("");
        StringBuilder newPwd=new StringBuilder("");
        if(mp.containsKey("t_id")){
            t_id.append(mp.get("t_id"));
        }
        if(mp.containsKey("t_password")){
            t_password.append(mp.get("t_password"));
        }
        if(mp.containsKey("newPwd")){
            newPwd.append(mp.get("newPwd"));
        }
        return teacherService.updatePwd(new Teachers(t_id.toString(),null,null,null,t_password.toString()),newPwd.toString());

    }

    @RequestMapping("file/showFile")
    public Result showFile(String token){
        return teacherService.showFile(token);
    }

    @RequestMapping("file/beforCorrectFile")
    public Result beforCorrectFile(String f_id){
        return teacherService.beforCorrectFile(f_id);
    }

    @RequestMapping("file/correctFile")
    public Result correctFile(@RequestBody Map<String,Object> mp){
        return teacherService.correctFile(mp);
    }

    @RequestMapping("showLogInfor")
    public Result showLogInfor(String token){
        return teacherService.showLogInfor(token);
    }

    @RequestMapping("sendMsg")
    public Result sendMsg(@RequestBody Map<String,Object>mp){
        return teacherService.sendMsg(mp);
    }

    @RequestMapping("showSendMsg")
    public Result showSendMsg(String token){
        return teacherService.showSendMsg(token);
    }

    @RequestMapping("showReciveMsg")
    public Result showReciveMsg(String token){
        return teacherService.showReciveMsg(token);
    }

    @RequestMapping("reply")
    public Result reply(@RequestBody Map<String,Object>mp){
        return teacherService.reply(mp);
    }

    @RequestMapping("downloadFile")
    public Result downloadFile(String file_id, HttpServletRequest req, HttpServletResponse resp) throws IOException {
        return teacherService.downloadFile(file_id,req,resp);

    }

    @RequestMapping("selectLeader")
    public Result selectLeader(@RequestBody Map<String,Object> mp) {
        return teacherService.selectLeader(mp);

    }
}
